package com.matou.auth.configure;


import com.matou.auth.properties.CmAuthProperties;
import com.matou.commons.handler.MaTouAccessDeniedHandler;
import com.matou.commons.handler.MaTouAuthExceptionEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

/**
 * @Author CM
 * @Date 2021/5/8 16:38
 * @Description CmResourceServerConfigure  资源服务器
 * @Version 1.0
 */
@Configuration
@EnableResourceServer
public class MaTouResourceServerConfigure extends ResourceServerConfigurerAdapter {

    @Autowired
    private MaTouAccessDeniedHandler accessDeniedHandler;

    @Autowired
    private MaTouAuthExceptionEntryPoint exceptionEntryPoint;

    @Autowired
    private CmAuthProperties cmAuthProperties;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .requestMatchers().antMatchers("/**")
                .and()
                .authorizeRequests()
                .antMatchers(cmAuthProperties.getAnonUrl()).permitAll()
                .antMatchers("/**").permitAll();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.authenticationEntryPoint(exceptionEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);
    }
}
